filename = "F:/python学习资料/Python-Machine-Learning-Cookbook-master/Chapter01/data_singlevar.txt"
X = []
y = []
with open(filename,'r') as f:
    for line in f.readlines():
        xt, yt = [float(i) for i in line.split(",")]
        X.append(xt)
        y.append(yt)

print(X[:5])
print(y[:5])
# 把数据划分为训练集和测试集
num_training = int(0.8*len(X))  # 80%的数据作为训练数据
num_test = len(X) - num_training  # 20%的数据作为测试数据

import numpy as np

# 训练数据
# 注意。模型要求输入数据X是二维向量，比如[[1],[3],[67]]这种
X_train = np.array(X[:num_training]).reshape(num_training,1)
y_train = np.array(y[:num_training])

# 测试数据
X_test = np.array(X[num_test:]).reshape(num_training,1)
y_test = np.array(y[num_test:])

from sklearn import linear_model

# 创建线性回归对象
linear_regressor = linear_model.LinearRegression()
# 使用训练集训练模型
linear_regressor.fit(X_train,y_train)

y_train_pred = linear_regressor.predict(X_train)

# 比较下真实值y_train与预测值y_train_pred的差别
import matplotlib.pyplot as plt
plt.figure()
plt.scatter(X_train,y_train,color='green')
plt.plot(X_train,y_train_pred,color='black',linewidth=4)
plt.title("Training data")
plt.show()

# 现在在测试集上查看运行效果
y_test_pred = linear_regressor.predict(X_test)
plt.figure()
plt.scatter(X_test,y_test,color='green')
plt.plot(X_test,y_test_pred,color='black',linewidth=4)
plt.title("Test data")
plt.show()

# 计算回归准确度
import sklearn.metrics as sm
print("Mean absolute err 平均绝对误差:",round(sm.mean_absolute_error(y_test,y_test_pred), 2))
print('Mean squared error 均方误差',round(sm.mean_squared_error(y_test,y_test_pred),2))
print("Median absolute error 中位数绝对误差",round(sm.median_absolute_error(y_test,y_test_pred),2))
print("explained variance score 解释方差分",round(sm.explained_variance_score(y_test,y_test_pred),2))
print("R2 score =",round(sm.r2_score(y_test,y_test_pred),2))

# 将模型持久化
import pickle

output_model_model_file = "3_model_linear_regr.pkl"
with open(output_model_model_file,'wb') as f:
    pickle.dump(linear_regressor,f)

# 使用持久化的模型
with open(output_model_model_file,"rb") as f:
    model_linregr = pickle.load(f)

y_test_pred_new = model_linregr.predict(X_test)
print("持久化模型的平均误差为",round(sm.mean_absolute_error(y_test,y_test_pred_new),2))
# pickle模块可以将任意的Python对象转化为字节序列，这一过程又称为对象的序列化

# 使用岭回归
print("-----使用岭回归----------")
from sklearn import linear_model
# alpha用于控制模型复杂度.
# fit_intercept表示是否需要截距,默认为True
# max_iter 最大迭代次数，即寻找损失函数最小值的迭代次数
ridge_regressor = linear_model.Ridge(alpha=0.01,fit_intercept=True,max_iter=10000)
ridge_regressor.fit(X_train,y_train)
y_test_pred_ridge = ridge_regressor.predict(X_test)
print("Mean absolute error =",
      round(sm.mean_absolute_error(y_test,y_test_pred_ridge),2))
print("R^2系数:",round(sm.r2_score(y_test,y_test_pred_ridge),2))
print(ridge_regressor.score(X_test,y_test))